package com.lkd.web.controller;


import com.lkd.entity.Goods;
import com.lkd.entity.ShoppingCart;
import com.lkd.mapper.ShoppingCartMapper;
import com.lkd.service.IShoppingCartService;
import com.lkd.service.utils.ResponseResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private IShoppingCartService shoppingCartService;

//购物车里商品删除(删)
    @GetMapping("/deleteShoppingCart")
    public @ResponseBody ResponseResult deleteShoppingCart(ShoppingCart arg){
        ShoppingCart shoppingCart = shoppingCartService.checkShoppingCart(arg);
        ResponseResult result = null;
        if (Objects.isNull(shoppingCart)){
            System.out.println("没有该商品");
            result = new ResponseResult<>(HttpStatus.NO_CONTENT.value(), "没有该商品");
        }else {
            shoppingCartService.deleteShoppingCart(shoppingCart);
            System.out.println("删除成功");
            result = new ResponseResult<>(HttpStatus.OK.value(), "删除成功", shoppingCart);
        }
        return result;
    }

//购物车里添加商品(增)
    @GetMapping("/insertGoods")
    public @ResponseBody int insertGoods(@Param("userId") Integer userId, @Param("goodsId") Integer goodsId) {
        ShoppingCart newshoppingCart = new ShoppingCart();
        newshoppingCart.setUserId(userId);
        newshoppingCart.setGoodsId(goodsId);
        newshoppingCart.setNumber(1);
        ShoppingCart shoppingCart = shoppingCartService.checkShoppingCart(newshoppingCart);
        if (Objects.isNull(shoppingCart)){
            return shoppingCartService.insertGoods(newshoppingCart);
        }else {
            return 0;
        }

    }

//购买数量加一(改)
    @GetMapping("/addGoods")
    public @ResponseBody Integer addGoods(@Param("userId") int userId, @Param("goodsId") int goodsId){

        return shoppingCartService.addGoods(userId,goodsId);
    }

//购买数量减一(改)
    @GetMapping("/downGoods")
    public @ResponseBody Integer downGoods(@Param("userId") int userId, @Param("goodsId") int goodsId) {
        ShoppingCart sc=new ShoppingCart();
        sc.setGoodsId(goodsId);
        sc.setUserId(userId);
        ShoppingCart shoppingCart=shoppingCartService.checkShoppingCart(sc);
        int number=shoppingCart.getNumber();
        if(number>1) {
            return shoppingCartService.downGoods(userId, goodsId);
        }else
        {
            return 0;
        }
    }

//级联查询出相应的明细信息(查)
    @GetMapping("/shoppingCartUsersGoods")
    public @ResponseBody Object shoppingCartUsersGoods(Integer userId,Integer goodsId){
        return shoppingCartMapper.shoppingCartUsersGoods(userId,goodsId);
        //http://localhost:8007/shoppingCart/shoppingCartUsersGoods?userId=1&goodsId=1
    }

//通过userId查询他购物车里的所有goods
    @RequestMapping("/queryUserGoods")
    public List<ShoppingCart> queryUserGoods(int userId) {
        return shoppingCartService.queryUserGoods(userId);
        //http://localhost:8007/shoppingCart/queryUserGoods?userId=1
    }

}
